package spring.service;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

import spring.bean.Person;

/**
 * @Description：
 * @author：涂有
 * @date 2015年11月8日 下午10:50:33
 */
@Transactional(rollbackFor=Exception.class)
public class PersonService {

	@Resource
	protected SessionFactory sessionFactory;
	
	public void save() throws Exception{
		
		Session session = sessionFactory.getCurrentSession();
		
		for(int i = 0; i< 50; i++){
			
			Person p = new Person();
			p.setName("tuyou");
			p.setAge(23);
			p.setBirth(new Date());
			InputStream input = PersonService.class.getResourceAsStream("bg.png");
			try {
				byte[] bytes = new byte[input.available()];
				input.read(bytes);
				p.setImage(bytes);
			} catch (IOException e) {
				e.printStackTrace();
			}
			p.setInfo("asdfasdjfiajqwipejfiqjdsnfpajweirqjpewipjreqw");
			
			session.save(p);
			
			if(i == 30){
				
				throw new Exception();
			}
		}
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
}
